package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatueCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import javafx.css.Styleable;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "02.车辆管理模块")
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;
    @ApiOperation("添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam,@ApiIgnore HttpSession session){
        log.debug("开始处理【添加车辆】的请求，参数：{}", vehicleAddParam);

        // 检查用户是否登录
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatueCode.NOT_LOGIN, "用户未登录");
//        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        vehicleMapper.insertVehicle(vehicle);
        return JsonResult.ok();
    }

    @GetMapping("list")
    @ApiOperation("车辆列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(VehicleListQuery vehicleListQuery, @ApiIgnore HttpSession session){
        log.debug("开始处理【车辆列表】的请求，参数：{}", vehicleListQuery);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatueCode.NOT_LOGIN, "用户未登录");
//        }
        String brand = vehicleListQuery.getBrand();
        String license = vehicleListQuery.getLicense();
        Long userId = userVO.getId();
        List<VehicleListVO> vehicleListVO= vehicleMapper.selectVehicle(brand,license,userId);
        return JsonResult.ok(vehicleListVO);
    }

    @PostMapping("del/{id}")
    @ApiOperation("删除车辆")
    @ApiOperationSupport(order = 30)
    @ApiImplicitParam(name = "id", value = "车辆id", required = true, dataType = "Long")
    public JsonResult del(@Range(min = 1,max = 100, message = "参数id不能小于0") @PathVariable Long id, @ApiIgnore HttpSession session){
        if (id<0){
            throw new IllegalArgumentException("参数id不能小于0");
        }
        log.debug("开始处理【删除车辆】的请求，参数：{}", id);
//        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatueCode.NOT_LOGIN, "用户未登录");
//        }
        int i=vehicleMapper.deleteVehicle(id);
        if (i>0){
            return JsonResult.ok();
        }
        return new JsonResult(StatueCode.OPERATION_FAILED,"该车辆不存在");
    }
}
